<template>
    <div class="RevocationClosingAccount topStyley">
        <el-card class="box-card">
            <div slot="headerss" class="clearfix text_fl">
                <span>公共服务类-->销户申请撤销</span>
                <el-button class="btn_close" type="text" @click="resetForm">关闭</el-button>
            </div>
            <div>
                <!-- <div class="tab-header">
                    <div class="headerTitles text_center">销户申请撤销</div>
                </div>-->
                <div>
                    <el-form
                        ref="formData"
                        :model="formData"
                        :inline="true"
                        :rules="rules"
                        label-width="150px"
                        label-position="right"
                        size="small"
                        class="formData_form_inline"
                    >
                        <el-row>
                            <el-col :span="11" :offset="1">
                                <el-form-item label="账户类别:" class="text_fl">
                                    <span class="el-form-item-span">{{isCATEGORY}}</span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="卡号:" class="text_fl">
                                    <span class="el-form-item-span">{{formData.CARDNO}}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="11" :offset="1">
                                <el-form-item label="销户至今时间:" class="text_fl">
                                    <span class="el-form-item-span">{{lastClsDiffDayStr}}</span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12"></el-col>
                        </el-row>

                        <!-- 话术 -->
                        <div class="hstable_wrap" v-if="speechList.length!=0">
                            <el-table
                                :data="speechList"
                                border
                                stripe
                                header-row-class-name="headerRow"
                                ref="showListTable"
                                class="heard-bg business_table hstable"
                                @selection-change="handleSelectionChange"
                            >
                                <el-table-column type="selection" label=" " min-width="55"></el-table-column>
                                <el-table-column label="业务话术">
                                    <template slot-scope="scope">{{scope.row.content}}</template>
                                </el-table-column>
                            </el-table>
                        </div>
                        <div class="text_center">
                            <el-button
                                type="primary"
                                size="small"
                                @click="isShowConfirm"
                                icon="el-icon-circle-check"
                            >确定</el-button>
                        </div>
                    </el-form>
                </div>
            </div>
        </el-card>
    </div>
</template>

<script>
import { mapGetters } from 'vuex';
import moment from 'moment';
import {
    trimString, closePage, getDictMappingValue, updateDeal3004, updateDeal3601,
} from '@/utils/Common';
import { isEmptyObject } from '@/utils/Object';

export default {
    name: 'RevocationClosingAccount',
    data() {
        return {
            formData: {
                CATEGORY: '', // 账户类别
                CARDNO: '', // 卡号
                IDTYPE: '', // //证件种类
                CUSTID: '', // 证件号码
                AUTOFLAG: '0', // 溢缴款自动结清申请
                CLSFLAG: '3', // 销户标志
                BANKFLAG: '0', // 销户自动结清转账行标志
            },
            rules: {
                BEGDATE: [{ required: true, message: '请选择菜单类型' }],
            },
            defaultParams: this.$store.getters.defaultParams || '', // 更新报文
            RevocationClosingAccountInfo: {}, // 撤销成功返回回来的数据
            isCATEGORY: '', //账户类型
            lastClsDiffDay: 0, //距离上次销户申请天数
            lastClsDiffDayStr: '', //距离上次销户申请天数显示，暂无/n天
            speechList: [],//话术列表
            selectedList: [],
            nowcardstat: '',

        };
    },
    computed: {
        ...mapGetters([
            'cardInfo_3004',
            'cardAccountInfo_3001',
            'pinFlag',
            'isCurrentMenuInfo',
            'checAllData',
        ]),

    },
    mounted() {
        this.pageEntryLimit();
        this.formData.CATEGORY = this.cardInfo_3004.CATEGORY;
        this.formData.CARDNO = this.cardInfo_3004.CARDNO;
        this.formData.IDTYPE = this.cardInfo_3004.KEYTYPE;
        this.formData.CUSTID = this.cardInfo_3004.CUSTID;
        this.getDictValue();
    },
    methods: {
        //页面进入限制条件
        pageEntryLimit() {
            let accountStat = this.cardAccountInfo_3001.CARDSTAT;
            console.info("=============pageEntryLimit,当前账户状态：" + accountStat);
            // if (accountStat != "Q") { //Q-持卡人请求关闭
            //     // 只有销户状态（持卡人请求关闭），才可打开该菜单页面
            //     this.$alert(
            //         "当前账户状态：" + accountStat + "，不是销户状态（持卡人请求关闭）,无法进入销户撤销页面！",
            //         '销卡提示',
            //         { customClass: 'warning', type: 'warning', showClose: false },
            //     ).then(() => {
            //         closePage(this.$route, this, 1);
            //     });
            // };

        },
        // 账户类型 字典取值
        async getDictValue() {
            this.isCATEGORY = await getDictMappingValue('CATE', this.formData.CATEGORY, this);
            // this.PRODUCT_value = await getDictMappingValue('PRODUCT',this.cardInfo_3004.PRODUCT,this);
            // 距上次销户时间
            var lastClsDay = this.cardAccountInfo_3001.LASTCLSDAY;
            console.info("===================lastClsDay", lastClsDay);

            // lastClsDay = 20210402 + ""; //测试用
            if (lastClsDay == 0) {
                this.lastClsDiffDayStr = "无";
            } else {
                const newTime = moment().format('YYYYMMDD')
                console.log('newTime', newTime);
                const diffDays = moment(newTime).diff(moment(lastClsDay + ""), 'days')
                console.log(diffDays, 'lastClsDayDiff');

                // var date2 = new Date();
                // var date1 = new Date(lastClsDay + "");
                // console.info("===================date1", date1);
                // console.info("===================date2", date2);
                // var diffDays = parseInt(Math.abs(date2 - date1) / 1000 / 60 / 60 / 24);

                this.lastClsDiffDay = diffDays;
                this.lastClsDiffDayStr = diffDays + "天";
            }
            await this.getspeechList();
        },
        //根据菜单id查询话术列表
        async getspeechList() {
            console.log("this.isCurrentMenuInfo.id", this.isCurrentMenuInfo.id);
            var checkParam = {
                'id': this.isCurrentMenuInfo.id,
            };
            this.$post.busiMenu.getlistByMenuId(checkParam).then((res) => {
                console.log("话术查询结果：", res);
                if (res.status === 200) { // http通讯成功
                    if (res.data.status == 0) { // 业务请求成功
                        this.speechList = res.data.content;
                    } else {
                        const ErrorMessage = `错误码：${res.data.status} ` + `错误信息：${res.data.msg}`;
                        this.$alert(ErrorMessage, '请求异常:', {
                            customClass: 'error', type: 'error',
                        });
                    }
                }
            });
        },
        handleSelectionChange(val) {
            this.selectedList = val;
        },
        isShowConfirm() {
            console.log("this.selectedList.length", this.selectedList.length);
            if (this.selectedList.length != this.speechList.length) {
                this.$alert('话术尚未全部勾选！', '提示', {
                    customClass: 'warning', type: 'warning',
                });
                return;
            }

            if (this.lastClsDiffDay >= 45) {
                this.$alert("销户至今时间>=45天，销户撤销失败！", "销户撤销", {
                    customClass: 'error', type: 'error',
                });
                return;
            }
            this.$confirm('你确定要撤销吗？', '销户申请提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                customClass: 'warning', type: 'warning',
            }).then(() => {
                this.submitForm();
            }).catch(() => false);
        },
        // 4011信用卡销户 销卡交易
        submitForm() {
            const defaultParam = JSON.parse(JSON.stringify(this.defaultParams));
            defaultParam.body.appHead.tranCode = '4011';// 修改交易号
            defaultParam.body.appBody = {
                PINFLAG: this.pinFlag,
                PIN: '',
                CARDNBR: this.formData.CARDNO,// 卡号
                IDTYPE: this.formData.IDTYPE,// 证件种类
                CUSTID: this.formData.CUSTID,// 证件号码，
                CLSFLAG: this.formData.CLSFLAG,// //销户标志
                AUTOFLAG: this.formData.AUTOFLAG, // //溢缴款自动结清申请
                CLSFLAG: this.formData.CLSFLAG, // 销户标志
                BANKFLAG: this.formData.BANKFLAG, // 销户自动结清转账行标志
            };
            this.$post.common.transPort(defaultParam).then((res) => {
                console.log(res);
                if (res.status === 200) { // 请求成功
                    const cardlossofs = JSON.parse(res.data.jsonRes);
                    console.log(cardlossofs, '4011信用卡销户撤销');
                    if (cardlossofs.sysHead.rspCd == '000000') { // 交易成功
                        this.RevocationClosingAccountInfo = cardlossofs.body.appBody;

                        // this.isShowSuccess=true;
                        updateDeal3004(this.cardInfo_3004.CARDNO, this);
                        updateDeal3601(this.cardInfo_3004.CARDNO, this.cardInfo_3004.KEYTYPE, this.cardInfo_3004.CUSTID, this);
                        const successMessage = `销户申请撤销成功！`;
                        const ts = `${cardlossofs.sysHead.busiName}提示`;
                        this.$alert(successMessage, ts, {
                            customClass: 'success', type: 'success',
                            callback: (action) => {
                                this.resetForm();
                            },
                        });
                        this.updateCheckData('Q', this.nowcardstat);
                    } else {
                        const ErrorMessage = `错误码：${cardlossofs.sysHead.rspCd} ` + `错误信息：${cardlossofs.sysHead.rspMsg}` + `\n错误描述：${cardlossofs.sysHead.rspDesc}`;
                        this.$alert(ErrorMessage, cardlossofs.sysHead.busiName, {
                            customClass: 'error', type: 'error',
                        });
                    }
                }
            });
        },
        updateCheckData(precardstat, nowcardstat) {
            const checAllDataItem = JSON.parse(JSON.stringify(this.checAllData));
            console.log(precardstat + "**************" + nowcardstat);
            precardstat = precardstat.trim() === '' ? '空' : precardstat.trim();
            nowcardstat = nowcardstat.trim() === '' ? '空' : nowcardstat.trim();
            checAllDataItem[precardstat].falg = false;
            checAllDataItem[nowcardstat].falg = true;

            // 修改验证开关 结束后再 保存一下 checAllDataItem
            this.$store.commit('common/SAVE_CHECK_DATA', checAllDataItem);
        },
        // 关闭页面
        resetForm() {
            closePage(this.$route, this, 1);
        },
    },
};
</script>

<style lang='scss' >
.RevocationClosingAccount {
    .el-card__header {
        padding: 8px !important;
    }

    .el-card.is-always-shadow {
        -webkit-box-shadow: none;
        box-shadow: none;
    }
    .el-card {
        border: none;
    }
}
</style>
